package com.sgb.kjwl.view.ui.activity.demand;

import android.content.Context;
import android.content.Intent;
import androidx.databinding.DataBindingUtil;
import android.os.Bundle;
import androidx.recyclerview.widget.LinearLayoutManager;
import android.view.View;
import android.widget.Toast;

import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ActivityDemandManageBinding;
import com.sgb.kjwl.di.home.DaggerHomeComponent;
import com.sgb.kjwl.di.home.HomeModule;
import com.sgb.kjwl.model.entity.NeedsEntity;
import com.sgb.kjwl.model.entity.ParentAreaEntity;
import com.sgb.kjwl.view.event.self.DemandEvent;
import com.sgb.kjwl.view.ui.adapter.home.MaterialSellerAdapter;
import com.sgb.kjwl.view.widget.AreaPopWindow3;
import com.sgb.kjwl.view.widget.CityPopWindow;
import com.sgb.kjwl.viewmodel.demand.DemandManageModel3;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.xrecyclerview.XRecyclerView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;

import javax.inject.Inject;

import static com.swgk.core.event.EventConstant.EVENT_ADD_GOOGS_CHANGE;
import static com.swgk.core.event.EventConstant.EVENT_ADD_GOOGS_DEL;
import static com.swgk.core.event.EventConstant.EVENT_ADD_GOOGS_DOWN;
import static com.swgk.core.event.EventConstant.EVENT_ADD_GOOGS_SUC;

/**
 * Description:
 * Author CFK
 * Time
 */
public class DemandManageActivity extends AppActivity {
    private ActivityDemandManageBinding binding;
    private LinearLayoutManager linearLayoutManager;
    private MaterialSellerAdapter materialSellerAdapter;
    private List<ParentAreaEntity<List<ParentAreaEntity>>> cityEntityList;
    private List<ParentAreaEntity> areaEntityList;
    private boolean isChange;
    //所在地区popWindow
    private CityPopWindow cityPopWindow;
    private AreaPopWindow3 areaPopWindow3;
    private String cityNo = "";
    private String areaNo = "";
    private String cityName = "";
    private String areaName = "";
    private int type;
    @Inject
    DemandManageModel3 viewModel;

    public static void start(Context context, int type) {
        Intent intent = new Intent();
        intent.putExtra("type", type);
        intent.setClass(context, DemandManageActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected boolean statusBarDark() {
        return true;
    }

    @Override
    protected void setupActivityComponent(AppComponent appComponent) {
        DaggerHomeComponent.builder().appComponent(appComponent).homeModule(new HomeModule(this)).build().inject(this);

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
    }

    @Override
    protected void initView() {
        type = getIntent().getIntExtra("type", 0);
        binding = DataBindingUtil.setContentView(DemandManageActivity.this, R.layout.activity_demand_manage);
        binding.include.ivBack.setOnClickListener(this);
        binding.cityLayout.setOnClickListener(this);
        binding.areaLayout.setOnClickListener(this);
        binding.include.rightView.setOnClickListener(this);
        if (type == 1) {
            binding.include.rightView.setText("重置");
            binding.include.titleView.setText("商品需求");
            binding.llArea.setVisibility(View.VISIBLE);
        } else {
            binding.include.rightView.setText("发布需求");
            binding.include.titleView.setText("我的需求");
        }
        binding.include.rightView.setVisibility(View.VISIBLE);


        //城市弹窗
        cityPopWindow = new CityPopWindow(getActivity());
        cityPopWindow.setBgView(binding.grayLayout);
        cityPopWindow.setOnResultClick((key1, key2, key3) -> {
            areaEntityList = (List<ParentAreaEntity>) key2;
            cityNo = (String) key1;
            cityName = (String) key3;
            binding.tvCity.setText(cityName);
//            showAreaPop();
            areaNo = "";
            binding.tvArea.setText("所在地区");
            viewModel.getNeedsList(cityNo, areaNo,type);
        });

        //地区弹窗
        areaPopWindow3 = new AreaPopWindow3(getActivity());
        areaPopWindow3.setBgView(binding.grayLayout);
        areaPopWindow3.setOnResultClick((key1, key2, key3) -> {
            areaNo = (String) key1;
            areaName = (String) key3;
            binding.tvArea.setText(areaName);
            viewModel.getNeedsList(cityNo, areaNo,type);
        });

        linearLayoutManager = new LinearLayoutManager(this);
        materialSellerAdapter = new MaterialSellerAdapter(this, null, type);
        binding.rv.setLayoutManager(linearLayoutManager);
        binding.rv.setAdapter(materialSellerAdapter);
        binding.rv.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                viewModel.getNeedsList(cityNo, areaNo,type);
            }

            @Override
            public void onLoadMore() {
                viewModel.getMoreList(cityNo, areaNo,type);
            }
        });
    }


    @Override
    protected void init() {
        viewModel.getNeedsList(cityNo, areaNo,type);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.city_layout:
                if (cityEntityList != null) {
                    showCityPop();
                } else {
                    viewModel.getDeviceAreaModel();
                }
                break;
            case R.id.area_layout:
                if (areaEntityList != null) {
                    showAreaPop();
                } else {
                    Toast.makeText(this, "请先选择城市", Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.right_view:
                if (type == 1) {
                    cityNo = "";
                    areaNo = "";
                    viewModel.getNeedsList(cityNo, areaNo,type);
                    binding.tvArea.setText("所在地区");
                    binding.tvCity.setText("所在城市");
                    areaEntityList = null;
                } else {
                    DemandPublishActivity.start(this, null);
                }
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onAttrEvent(DemandEvent event) {
        if (event.getMessageTag() == EVENT_ADD_GOOGS_SUC) {
            viewModel.getNeedsList(cityNo, areaNo,type);
        } else if (event.getMessageTag() == EVENT_ADD_GOOGS_DEL) {
            viewModel.getNeedsList(cityNo, areaNo,type);
        } else if (event.getMessageTag() == EVENT_ADD_GOOGS_DOWN) {
            viewModel.getNeedsList(cityNo, areaNo,type);
        } else if (event.getMessageTag() == EVENT_ADD_GOOGS_CHANGE) {
            viewModel.getNeedsList(cityNo, areaNo,type);
        }
    }

    private void showAreaPop() {
        areaPopWindow3.showPopwindow(binding.areaLayout);
        areaPopWindow3.setData(areaEntityList, isChange,areaNo);
        isChange = false;
    }

    private void showCityPop() {
        cityPopWindow.showPopwindow(binding.cityLayout);
        cityPopWindow.setData(cityEntityList, isChange);
        isChange = false;
    }

    public void showCityPop(List<ParentAreaEntity<List<ParentAreaEntity>>> datas) {
        this.cityEntityList = datas;
        showCityPop();
    }

    public void refresh(List<NeedsEntity> datas, boolean isLast) {
        binding.emptyView.setVisibility(View.GONE);
        binding.rv.setVisibility(View.VISIBLE);
        binding.rv.setLoadingMoreEnabled(true);
        materialSellerAdapter.setDatas(datas);
        binding.rv.refreshComplete();
        if (isLast) {
            binding.rv.setLoadingMoreEnabled(false);
        }
    }

    public void empty() {
        binding.rv.refreshComplete();
//        binding.rlvView.setVisibility(View.GONE);
        materialSellerAdapter.setDatas(null);
        binding.emptyView.setVisibility(View.VISIBLE);
    }

    public void loadMore(List<NeedsEntity> datas, boolean isLast) {
        materialSellerAdapter.addDatas(datas);
        binding.rv.loadMoreComplete();
        if (isLast) {
            binding.rv.setLoadingMoreEnabled(false);
        }
    }
}
